package com.xiaoyu.lanling.widget.momentgrid;

import com.xiaoyu.base.image.ImageLoadInfo;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.media.image.ImageLoadParam;

import java.util.ArrayList;

import in.srain.cube.util.LocalDisplay;
import in.srain.cube.views.list.ListItemTypedBase;

public class MomentGridItem implements ListItemTypedBase {

    private static final int CORNER_RADIUS = LocalDisplay.dp2px(4);

    public final String imageUrl;
    public final ImageLoadParam mImageLoadParam;
    public final ArrayList<String> imageUrlList;
    public final int itemWidth;
    public final int itemHeight;
    public String feedId;
    public MomentGridItem(String imageUrl, ArrayList<String> imageUrlList) {
        this.imageUrl = imageUrl;
        this.imageUrlList = imageUrlList;
        this.itemHeight = getHeightInPX(imageUrlList.size());
        this.itemWidth = getWidthInPX();
        this.mImageLoadParam = ImageLoadParam.newBuilder()
                .setHeight(itemHeight)
                .setWidth(itemWidth)
                .setUrl(imageUrl)
                .setCornersRadius(CORNER_RADIUS)
                .setPlaceHolderResId(R.drawable.icon_normal_empty)
                .build();
    }

    @Override
    public int getViewType() {
        return 0;
    }

    private int getHeightInPX(int listSize) {
        switch (listSize) {
            case 1:
                return MomentGridLayoutView.GRID_ITEM_SIZE_WITH_PX_FOR_ONE;
            case 2:
                return MomentGridLayoutView.GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW_FOR_TWO;
            default:
                return MomentGridLayoutView.GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW;
        }
    }

    private int getWidthInPX() {
        if (imageUrlList.size() == 1) {
            ImageLoadInfo loadInfo = new ImageLoadInfo(imageUrl);
            if (loadInfo.getRatio() < 0.75F) {
                return (int) (MomentGridLayoutView.GRID_ITEM_SIZE_WITH_PX_FOR_ONE * (0.75F));
            } else if (loadInfo.getRatio() < 1) {
                return (int) (MomentGridLayoutView.GRID_ITEM_SIZE_WITH_PX_FOR_ONE * loadInfo.getRatio());
            } else {
                return MomentGridLayoutView.GRID_ITEM_SIZE_WITH_PX_FOR_ONE;
            }
        } else {
            return itemHeight;
        }
    }
}
